package com.focus.xyz.other.pages.njhrss;

import com.focus.xyz.core.common.keyword.Action;
import com.focus.xyz.core.utils.Constants;
import com.focus.xyz.core.utils.ExcelUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * Created by chenmeng on 2016/4/28.
 */
public class DataPage{

    @FindBy(id = "Submit2")
    private WebElement queryBtn;
    @FindBy(xpath = "//a[contains(text(),'下一页')]")
    private WebElement nextPage;
    @FindBy(xpath = "/html/body/table[4]/tbody/tr/td/table[2]/tbody")
    private WebElement table;
    @FindBy(name = "Submit")
    private WebElement queryBt;
    @FindBy(name = "resout0")
    private WebElement input;

    private String mainUrl = "http://wsbs.njhrss.gov.cn/NJLD/ForWard?act=ForWardLes&id=105&lx=n";
    private String mainUrl1 = "http://wsbs.njhrss.gov.cn/NJLD/ForWard?act=ForWardLes&id=108&lx=n";

    private static Action action;

    public DataPage(WebDriver driver,Action action){
        this.action = action;
        PageFactory.initElements(driver,this);
    }

    public void openPage(){
        try {
            action.open(mainUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void next() throws Exception{
        action.click(nextPage);
        action.waitForPageLoad();
    }

    public void query() throws Exception{
        action.click(queryBtn);
        action.waitForPageLoad();
    }

    public void qryByCond(String name) throws Exception{
        action.inputText(input,name);
        action.click(queryBt);
        action.waitForPageLoad();
    }

    //支付类别为乙类时 更新
    public void modify(ExcelUtils eu, String rowNum, String tym, String spm, String bzlx) throws Exception{
        List<WebElement> rows = table.findElements(By.tagName(Constants.TAG_TR));
        int[] aa ={5,6,7};
        if(StringUtils.isBlank(bzlx)){
            aa = new int[]{3, 5, 6, 7};
        }
        for(WebElement r : rows){
            List<WebElement> cols = r.findElements(By.tagName(Constants.TAG_TD));
            if(cols.get(0).getText().trim().equals(tym)&&cols.get(1).getText().trim().equals(spm)){
                String type = cols.get(2).getText().trim();
                String rate = cols.get(4).getText().trim();
                String canSell = cols.get(5).getText().trim();
                String desc = cols.get(6).getText().trim();
                String[] bb = {rate,canSell,desc};
                if(StringUtils.isBlank(bzlx)){
                    bb = new String[]{type,rate,canSell,desc};
                }
                eu.setCells(bb,Integer.valueOf(rowNum),aa);
                break;
            }
        }

    }

    //支付类别为空时 更新所有
    public void modify1(ExcelUtils eu, String rowNum, String tym, String spm) throws Exception{
        List<WebElement> rows = table.findElements(By.tagName(Constants.TAG_TR));
        int[] aa ={3,4,5,6,7};
        for(WebElement r : rows){
            List<WebElement> cols = r.findElements(By.tagName(Constants.TAG_TD));
            if(cols.get(0).getText().trim().equals(tym)&&cols.get(1).getText().trim().equals(spm)){
                String type = cols.get(2).getText().trim();
                String bzjx = cols.get(3).getText().trim();
                String rate = cols.get(4).getText().trim();
                String canSell = cols.get(5).getText().trim();
                String desc = cols.get(6).getText().trim();
                String[] bb = {type,bzjx,rate,canSell,desc};
                eu.setCells(bb,Integer.valueOf(rowNum),aa);
                break;
            }
        }
    }

    public void export() throws Exception{
        List<WebElement> rows = table.findElements(By.tagName(Constants.TAG_TR));
        writeRows("E:\\njhrss.xlsx",rows);
        if(action.isElementExsit(By.xpath("//a[contains(text(),'下一页')]"))){
            next();
            export();
        }
    }

    public void writeRows(String path,List<WebElement> rows){
        File file = new File(path);
        FileOutputStream fos = null;
        try{
            if(file.exists()){
                Workbook wb = new XSSFWorkbook(new FileInputStream(file));
                Sheet sheet = wb.getSheet("Data2");
                int end = sheet.getLastRowNum();
                for(int i = 1 ; i< rows.size(); i++){
                    Row newRow = sheet.createRow(end );
                    if(!rows.get(i).getText().contains("下一页")){
                        List<WebElement> cells = rows.get(i).findElements(By.tagName(Constants.TAG_TD));
                        for(int j = 0;j<cells.size();j++){
                            newRow.createCell(j).setCellValue(cells.get(j).getText());
                        }
                        end ++;
                    }
                }
                fos = new FileOutputStream(path);
                wb.write(fos);
                fos.flush();
                fos.close();
            }
        }catch (Exception e){

        }finally {
            try {
                if(fos != null){
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
